/*
 * Created on Oct 9, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package schoolsource.model;

import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Contains information for parents and students.
 */
public class PairedUserContainer extends UserContainer {

	// The map of timer buckets for all databases
	private static Hashtable dbStores = new Hashtable();
	// The auto-generated id of the other associated person (i.e. child or parent)
	IntegerItem otherID = null;

	/**
	 * Default constructor.
	 */
//	protected PairedUserContainer() {
//		super();
//System.out.println("@@@@@@@@@@@2");
//	}

	/**
	 * Returns a new instance of the container with values pre-loaded from the database
	 * according to the passed-in user ID.
	 * @param newUserId The auto-generated ID of the user.
	 */
	protected PairedUserContainer(String newUserId, String dbName) {
		super(newUserId, dbName);
	}

	public static PairedUserContainer getPairedContainer(String pID, String dbName) {
		PairedUserContainer returnVal = null;
		DBStore dbs = null;

		if (pID == "-1") {
			returnVal = new PairedUserContainer(pID, dbName);
		}
		else {
			if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
				dbs = new DBStore();
				dbStores.put(dbName, dbs);
				dbs.start();
			}
			if ((returnVal = (PairedUserContainer) dbs.getContainer(pID)) == null) {
				returnVal = new PairedUserContainer(pID, dbName);
				returnVal.getAssignedID().setValue(pID);
				SchoolDBSQL.getSchoolDBSQL(dbName).loadUser(returnVal.getValidationItems(), returnVal.getAssignedID());
				dbs.addContainer(returnVal, pID);
			}
		}

		return returnVal;
	}

	/**
	 * Returns the ID of the student in the parent/student pair.
	 * @return The auto-generated ID of the student.
	 */
	// TODO fix this
	public IntegerItem getStudentID() {
		IntegerItem returnVal = null;
		if (getType().toString().equals(SchoolDBSQL.PARENT)) {
			if (otherID == null) {
				otherID =
					new IntegerItem(
						SchoolDBSQL.USRASSIGNEDID,
						SchoolDBSQL.getSchoolDBSQL(
							schoolName).getAssignedID(
							getSchoolID().toString(),
							SchoolDBSQL.STUDENT),
						false);
			}
			SchoolLogger.logger.log(
				Level.INFO,
				" - paireduser is parent (other ID): " + otherID);
			returnVal = otherID;
		} else {
			SchoolLogger.logger.log(
				Level.INFO,
				" - paireduser is student (assigned ID): " + getAssignedID());
			returnVal = getAssignedID();
		}

		return returnVal;
	}

	/**
	 * Returns the ID of the parent in the parent/student pair.
	 * @return The auto-generated ID of the parent.
	 */
	// TODO fix this
	public IntegerItem getParentID() {
		IntegerItem returnVal = null;
		if (getType().toString().equals(SchoolDBSQL.STUDENT)) {
			if (otherID == null) {
				otherID =
					new IntegerItem(
						SchoolDBSQL.USRASSIGNEDID,
						SchoolDBSQL.getSchoolDBSQL(
							schoolName).getAssignedID(
							getSchoolID().toString(),
							SchoolDBSQL.PARENT),
						false);
			}
			returnVal = otherID;
		} else {
			returnVal = getAssignedID();
		}

		return returnVal;
	}

	/**
	 * Returns the ID of the student.
	 * @return The auto-generated ID of the student.
	 */
	public IntegerItem getIDForClasses() {
		return getStudentID();
	}

}
